<%@ page language="java" pageEncoding="us-ascii" contentType="text/html;charset=us-ascii" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%--Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
	 
	This file is part of CDinFusion.
	
	CDinFusion is free software: you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public 
	License in version 3 (LGPL3) as published by the Free Software
	Foundation version 3 of the License.
	
	CDinFusion is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	Lesser General Public License for more details.
	
	You should have received a copy of the Lesser General Public
	License along with CDinFusion; see the file COPYING. If not,
	write to the Free Software Foundation, 59 Temple Place - Suite 330,
	Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
	
	If you have any questions, send an email to: megx@mpi-bremen.de.
--%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="org.cdinfusion.data.*"%>
<%@ page import="org.cdinfusion.data.environment.*"%>
<%@ page import="org.cdinfusion.data.studytype.*"%>
<%@ page import="java.util.List"%>
<%
System.out.println("<<<<<<<<<<3b2_CD_INPUT_ntom>>>>>>>>>>");

String study = "";
String env = "";
String old_study = "";
String old_env = "";
String message = "";
String origin = (String) session.getAttribute("origin");
String sID = "";
int id_num = 10;
int gID = - 1;
CDElement cdelement = null;
Object obj1 = null;
Object obj2 = null;
Object obj3 = null;

boolean error = false;
boolean keepheader = false;
boolean old_keepheader = false;
boolean fillEnv = false;
boolean fillStudy = false;
boolean fillFasta = false;
String context_exists = (String) session.getAttribute("context_exists");
int selgroup = 0;

List<CDElement> cdeLIST = null;
List<String> uSeqID = new ArrayList<String>();
List<String> sSeqID = new ArrayList<String>();
List<String> allSequenceIDs = (List<String>) session.getAttribute("allSequenceIDs");

try{
	if(origin.equals("2b")|origin.equals("3b1")|origin.equals("3b2_csv")){//!exist
		cdeLIST = (List<CDElement>) session.getAttribute("cdeLIST");
		study = cdeLIST.get(cdeLIST.size()-1).getStudy().getClass().toString().toLowerCase().substring(36); 
		env = cdeLIST.get(cdeLIST.size()-1).getEnv().getClass().toString().toLowerCase().substring(38);
	}//end if(origin.equals("2b")|origin.equals("3b1")|origin.equals("3b2_csv"))
		
	if(context_exists.equals("yes") & origin.equals("3b1")){
		List<CDElement> oldLIST = (List<CDElement>) session.getAttribute("oldlist");
	}//end if(exist & origin.equals("3b1"))
		
	if(origin.equals("3b2_csv")){
		uSeqID = (List<String>) session.getAttribute("uSeqID");
		sSeqID = (List<String>) session.getAttribute("sSeqID");
		cdeLIST = (List<CDElement>) session.getAttribute("cdeLIST");
		cdelement = cdeLIST.get(cdeLIST.size()-1);
		keepheader = cdelement.isKeepheader();
		obj1 = cdelement.getStudy();
		obj2 = cdelement.getEnv();
		obj3 = cdelement.getCDFastaheader();
		
		fillEnv = true;
		fillStudy = true;
		fillFasta = true;
		keepheader = cdelement.isKeepheader();
	}//end if(origin.equals("3b2_csv"))
		
	if(origin.equals("uploadcsv")){
		cdeLIST = (List<CDElement>) session.getAttribute("cdeLIST");
		study = cdeLIST.get(cdeLIST.size()-1).getStudy().getClass().toString().substring(36).toLowerCase();
		env = cdeLIST.get(cdeLIST.size()-1).getEnv().getClass().toString().substring(38).toLowerCase();
		uSeqID = (List<String>) session.getAttribute("seqIDs");
		for(int i = 0; i < allSequenceIDs.size(); i++){
			for(int j = 0; j < uSeqID.size(); j++){
				if(uSeqID.get(j).equals(allSequenceIDs.get(i).trim())){
					sSeqID.add(allSequenceIDs.get(i).trim());
				}
			}
		}
		
		cdelement = cdeLIST.get(cdeLIST.size()-1);
		obj1 = cdelement.getStudy();
		obj2 = cdelement.getEnv();
		obj3 = cdelement.getCDFastaheader();
		
		fillEnv = true;
		fillStudy = true;
		fillFasta = true;
		keepheader = cdelement.isKeepheader();
	}
	
	if(!origin.equals("cd_change")){
		origin = "3b2";
	}
	
	if(origin.equals("cd_change")){
		cdeLIST = (List<CDElement>) session.getAttribute("cdeLIST");
		String cSeqID = (String) session.getAttribute("cSeqID");

		gID = (int) Integer.parseInt(cSeqID);
		session.setAttribute("gID", gID);
		cdelement = cdeLIST.get(gID);

		obj1 = cdelement.getStudy();
		study = cdelement.getStudy().getClass().toString().substring(36).toLowerCase();
		obj2 = cdelement.getEnv();
		env = cdelement.getEnv().getClass().toString().substring(38).toLowerCase();
		obj3 = cdelement.getCDFastaheader();
		
		fillEnv = true;
		fillStudy = true;
		fillFasta = true;
		keepheader = cdelement.isKeepheader();
	}

	session.setAttribute("origin", origin);
	session.setAttribute("sSeqID", sSeqID);
	session.setAttribute("uSeqID", uSeqID);	
		
}catch(Exception e){
	System.out.println("Exception in 3b2: "+e.getMessage()+" "+e.toString());
	System.out.println("Exception in 3b2: "+e.getLocalizedMessage());
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head> <title>DIRECT CONTEXTUAL DATA INPUT n TO m</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<style type="text/css">
</style>
<jsp:include page="googleanalytics.jsp"/>

<script type="text/javascript" src="/CDinFusion/js/numValues.js"></script>
</head>

<body>
<table border="0" width="100%" bgcolor="#2B3990" cellpadding=0 cellspacing=0>
<tr bgcolor="#ffffff">
<td width=250>
<center>
<img height="81" width="250" alt="CDinFusion Logo" src="/CDinFusion/img/logo_small_dark.png">
</center>
</td>
</tr>

<tr align="left">    
<td>
<jsp:include page="documentation/navigation.jsp"></jsp:include>
</td>
</tr>

<tr bgcolor="#ffffff">
<td>
<div class="center_div">    
<br>
<center><b> Enter contextual data</b> <br>
</center>
<center>
<form name="Formular" action="CD_entered_many.htm" method="post" accept-charset="us-ascii" onsubmit="return numValues()">
<%session.setAttribute("initial", "false"); %>
	<table>
		<tr><td><%
		//always
		if(!origin.equals("cd_change")){%>
			<p>The group contains <strong><%=cdeLIST.get(cdeLIST.size() - 1).getNumberOfSequences()%></strong> sequence IDs, they start with <br>
			the ID <strong><%=cdeLIST.get(cdeLIST.size() - 1).getFirstSequenceID()%></strong> and end with <strong><%=cdeLIST.get(cdeLIST.size() - 1).getLastSequenceID()%></strong>.
			</p><%
		}else{ %>
			<p>The group contains <strong><%=cdeLIST.get(gID).getNumberOfSequences()%></strong> sequence IDs, they start with <br>
			the ID <strong><%=cdeLIST.get(gID).getFirstSequenceID()%></strong> and end with <strong><%=cdeLIST.get(gID).getLastSequenceID()%></strong>.
			</p><%
		}%>
		</td></tr>
		<tr><td>
	<%try{ %>
		<table style="100%">
  			<tr>
  			<td>
		    <div style="overflow:auto; height: 400px;">
		    
			<%
			if(fillFasta){
				CDFastaHeader cdfheader = (CDFastaHeader) obj3;
				%>
				<b>information, that will be included to the FASTA header line:</b>
				<table width="100%" border="0" bgcolor="#DCDCDC" cellpadding="1">
			
				<tr>
				<td><b><font size=2>parameter</font></b></td>
				<td style="min-width:530px"><b><font size=2></font></b></td>
				<td><b><font size=2>value</font></b></td>
				</tr>
			
				<tr><td><b><font size=1>authority: </font></b></td>
			    <td><font size=1>The author or authors of the organism name from which sequence was obtained.</font></td>
				<td align="right"><input type="text" name="authority" class="required parameter" size="25" value="<%=cdfheader.getAuthority()%>"></td></tr>
				
				<tr><td><b><font size=1>collected by: </font></b></td>
			    <td><font size=1>Name of person who collected sample. Do not use accented or non-ASCII characters.</font></td>
				<td align="right"><input type="text" name="collectedby" class="required parameter" size="25" value="<%=cdfheader.getCollectedby()%>"></td></tr>
				
				<tr><td><b><font size=1>organism name: </font></b></td>
				<td><font size=1>taxonomic name of the sequenced organism, if unknown, e.g. <br> uncultured bacterium, uncultured archaeon, uncultured eukaryote, or uncultured organism</font></td>
				<td align="right"><input type="text" name="organism" class="required parameter" size="25" value="<%=cdfheader.getOrganism()%>"></td></tr>
				
				<tr><td><b><font size=1>isolation source: </font></b></td>
		    	<td><font size=1>ame of the sampling site/geographic location.</font></td>
				<td align="right"><input type="text" name="isolation_source" class="required parameter" size="25" value="<%=cdfheader.getIsolation_source()%>"></td></tr>
			
				</table><%
			}//end if(fillFasta)
			if(!fillFasta){%>
				<b>information, that will be included to the FASTA header line:</b>
				<table width="100%" border="0" bgcolor="#DCDCDC" cellpadding="1">
			
				<tr>
				<td><b><font size=2>parameter</font></b></td>
				<td style="min-width:530px"><b><font size=2></font></b></td>
				<td><b><font size=2>value</font></b></td>
				</tr>
			
				<tr><td><b><font size=1>authority: </font></b></td>
			    <td><font size=1>The author or authors of the organism name from which sequence was obtained.</font></td>
				<td align="right"><input type="text" name="authority" class="required parameter" size="25" value=""></td></tr>
				
				<tr><td><b><font size=1>collected by: </font></b></td>
			    <td><font size=1>Name of person who collected sample. Do not use accented or non-ASCII characters.</font></td>
				<td align="right"><input type="text" name="collectedby" class="required parameter" size="25" value=""></td></tr>
				
				<tr><td><b><font size=1>organism name: </font></b></td>
				<td><font size=1>taxonomic name of the sequenced organism, if unknown, e.g. <br> uncultured bacterium, uncultured archaeon, uncultured eukaryote, or uncultured organism</font></td>
				<td align="right"><input type="text" name="organism" class="required parameter" size="25" value=""></td></tr>
				
				<tr><td><b><font size=1>isolation source: </font></b></td>
		   		<td><font size=1>name of the sampling site/geographic location.</font></td>
				<td align="right"><input type="text" name="isolation_source" class="required parameter" size="25" value=""></td></tr>
				
				</table><%
			}//end if(!fillFasta)
			
//<!-- studytype -->
			if(fillStudy){
				session.setAttribute("obj1", obj1);
				session.setAttribute("study", study);
				%>
				<jsp:include page="3_filled_study_input_fields.jsp"/>
				<%
			} //end if(fillStudy)
			if (!fillStudy){
				%>
				<jsp:include page="3_empty_study_input_fields.jsp" >
					<jsp:param name="study" value="<%=study%>" />
				</jsp:include>
				<%
			} //end if (!fillStudy)

//<!-- env_packages --> 		  
			if(fillEnv){
				session.setAttribute("obj2", obj2);
				session.setAttribute("env", env);
				%>
				<jsp:include page="3_filled_env_input_fields.jsp" />
				<%
			} //end if(fillEnv)
			if(!fillEnv){
				%>
				<jsp:include page="3_empty_env_input_fields.jsp" >
					<jsp:param name="env" value="<%=env%>" />
				</jsp:include>
				<%
			} //end if(!fillEnv)
			%>
			
			</div>
			</td>
			</tr>
			<tr>
			    <td>Would you like to get a new FASTA header?
			    <select name="KeepFASTAHeader" size="1"> 
      			<%
      			if(keepheader == true){%>
      				<option selected value="true">no</option>
      			<%}
      			else{%>
      				<option value="true">no</option>
      			<%}
      			if(keepheader == false){%>
      				<option selected value="false">yes</option>
      			<%}
      			else{%>
      				<option value="false">yes</option>
      			<%}%>
				</select></td>
      		</tr>
		</table>
	<%}catch(Exception e){
		error = true;
		message = message +"\n ERROR IS: "+ e.getMessage();
	}%>
		download the entry as contextual data spreadsheet: <input class="submitbutton" name="submit" type="submit" value="download filled csv">
		</td></tr>
		<tr align=center><td>
		<input class="submitbutton" name="submit" type="submit" value="proceed">
		</td></tr>
	</table>
</form>
</center>
</div>
</td>
</tr>
<tr bgcolor="#ffffff"><td align="right" height=30>
	<img height="20" width="200" alt="CDinFusion Step 3" src="/CDinFusion/img/step3.png">
</td></tr>
<jsp:include page="documentation/bottom_info.jsp"></jsp:include>
</table>
</body>
</html>